﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using web.core.ViewModels;
using web.core.Models;
using web.component; 

namespace web.core.Controllers
{
    public class admLanguageController : baseController
    {
        //------------------------------------
        //language_list
        //------------------------------------
        #region language_list PAGE
        public ActionResult language_list()
        {
            bool permission = false;
            InitPage(ref permission);
            ViewBag.permission = permission;
            return View();
        }

        [ChildActionOnly]
        [HttpGet]
        public ActionResult get_list(int? pageindex)
        {
            return get_list(pageindex, admin_const.PAGE_LIST_SIZE);
        }

        [ChildActionOnly]
        [HttpPost]
        public ActionResult get_list(int? pageindex, int? pagesize)
        {  
           var page_view = new ViewModels.LanguagePageListViewModels()
           {
               //list_group = accountModels.getListUserGroup(),
               list_pagesize = GetSizePagingPublic(pagesize ?? admin_const.PAGE_LIST_SIZE),
               pagesize = pagesize ?? admin_const.PAGE_LIST_SIZE,
               page_list_language = Models.languageModels.adm_lang_cate_list( pageindex ?? 1, pagesize ?? admin_const.PAGE_LIST_SIZE, -1)
           };
           return PartialView("../Shared/language/get_list", page_view);
        }

        public ActionResult updateStatus(string id)
        {
            int language_id = int.Parse(id.Split(',')[0]);
            byte language_status = byte.Parse(id.Split(',')[1]);
            Models.languageModels.adm_lang_enable(language_id, language_status);
            string outStr = "" + language_status.ToString();
            return Json(outStr);
        }

        #endregion

        //------------------------------------
        //language_edit 
        //------------------------------------
        #region language_edit PAGE
        public ActionResult language_edit()
        {
            bool permission = false;
            InitPage(ref permission);
            ViewBag.permission = permission;
            return View();
        }

        [ChildActionOnly]
        [HttpGet]
        public ActionResult ctr_lang_edit(int? id)
        {
            int itemid = id ?? 0;
            if (itemid == 0)
                ViewBag.isAdd = true;
            else
                ViewBag.isAdd = false;

            ViewModels.LanguageViewModels item_view;
            //var item_view = new ViewModels.LanguageViewModels();
            var it = languageModels.getByID(itemid, "");
            if (it != null)
            {
                item_view = new LanguageViewModels()
                {
                    language_id = it.language_id,
                    language_code = it.language_code,
                    language_flag = it.language_flag,
                    language_desc = it.language_desc,
                    language_status = Convert.ToByte(it.language_status)
                };
            }
            else
                item_view = new LanguageViewModels();

            return PartialView("../Shared/language/lang_add", item_view);
        }

        [ChildActionOnly]
        [HttpPost]
        public ActionResult ctr_lang_edit(FormCollection collection)
        {
            var item_view = new ViewModels.LanguageViewModels();
            TryUpdateModel(item_view);

            if (string.IsNullOrEmpty(item_view.language_code) || string.IsNullOrEmpty(item_view.language_desc))
            {
                ViewBag.result_message = "Tên và mã ngôn ngữ không để trống.";
                ViewBag.result = false;
                return PartialView("../Shared/language/lang_add", item_view);
            }
            var item = new data.entities.language()
            {
                language_id = item_view.language_id,
                language_code = item_view.language_code,
                language_desc = item_view.language_desc,
                language_flag = item_view.language_flag,
                language_status = item_view.language_status
            };
            
            int ret_id = languageModels.adm_lang_update(item);

            if (ret_id > 0)
            {
                ViewBag.result_message = admin_const.UPDATE_OK;
                ViewBag.redirect = "/vi/admLanguage/language_list";
                return PartialView("../Shared/control_redirect");
            }
            else
            {
                ViewBag.result_message = admin_const.UPDATE_FAIL;
                ViewBag.result = false;
                return PartialView("../Shared/language/lang_add", item);
            }
        }
        #endregion
    }
}